package com._520it.day1._03_dml;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import org.junit.Test;

public class DMLDemo {
	
	/**
	 * 向t_student表中插入一条数据1，张三，17
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception {
		//加载驱动，获取连接对象，创建语句对象，执行sql，释放资源
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "admin");
		String sql="INSERT INTO t_student(name,age) VALUES('张三',17)";
		Statement st = conn.createStatement();
		st.execute(sql);
		st.close();
		conn.close();
	}
	
	/**
	 * 将id为1的学生的姓名修改为李四
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception {
		//加载驱动，获取连接对象，创建语句对象，执行sql，释放资源
				Class.forName("com.mysql.jdbc.Driver");
				Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "");
				String sql="UPDATE t_student SET age=18 WHERE id=2" ;
				Statement st = conn.createStatement();
				st.execute(sql);
				st.close();
				conn.close();
	}
	
	/**
	 * 删除一条数据
	 * @throws Exception
	 */
	@Test
	public void test3() throws Exception {
		//加载驱动，获取连接对象，创建语句对象，执行sql，释放资源
				Class.forName("com.mysql.jdbc.Driver");
				Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbcdemo", "root", "");
				String sql="DELETE FROM t_student WHERE id=1";
				Statement st = conn.createStatement();
				st.execute(sql);
				st.close();
				conn.close();	
	}
}
